<template>
  <a-spin :spinning='confirmLoading'>
    <j-form-container :disabled='formDisabled'>
      <a-form :form='form' slot='detail'>
        <a-row>
          <a-col :span='12'>
            <a-form-item label='项目简称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-popup
                v-decorator="['projectName',validatorRules.projectShortName]"
                :trigger-change='true'
                org-fields='id,short_name'
                dest-fields='projectId,projectName'
                code='pm_project_select'
                @callback='popupCallback'
                :disabled='isAdd===2'
              />
              <input v-decorator="['projectId']" v-show='false'></input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='专题名称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input
                v-decorator="['subjectName', validatorRules.subjectName]"
                placeholder='请输入专题名称'
                :disabled='isAdd===2'
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='预计开始时间' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-date placeholder='请选择预计开始时间' v-decorator="['playStartTime',validatorRules.playStartTime]"
                      :trigger-change='true'
                      :disabled='isAdd===2'
                      style='width: 100%' />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='预计结束时间' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-date placeholder='请选择预计结束时间' v-decorator="['playEndTime',validatorRules.playEndTime]"
                      :disabled='isAdd===2'
                      :trigger-change='true' style='width: 100%' />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='预估人天' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input-number :min='0' v-decorator="['estimateWorkday',validatorRules.estimateWorkday]"
                              placeholder='请输入预估人天' style='width: 100%'
                              :disabled='isAdd===2'
              />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='状态' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-dict-select-tag type='list' v-decorator="['subjectState',validatorRules.subjectState]"
                                 :trigger-change='true' dictCode='question_state' />
            </a-form-item>
          </a-col>
          <a-col :span='24'>
            <a-form-item label='专题描述' :labelCol='labelCol2' :wrapperCol='wrapperCol2'>
              <a-textarea :autoSize='{minRows:4}' v-decorator="['subjectDes',validatorRules.subjectDes]"
                          placeholder='请输入专题描述'
                          :disabled='isAdd===2'
              />
            </a-form-item>
          </a-col>
          <a-col :span='12' v-if='isAdd===2'>
            <a-form-item label='实际人天' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input-number :min='0' v-decorator="['realityWorkday',validatorRules.realityWorkday]"
                              placeholder='请输入实际人天' style='width: 100%'
              />
            </a-form-item>
          </a-col>
          <a-col :span='12' v-if='isAdd===2'>
            <a-form-item label='确认人' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <a-input v-decorator="['confirmorName',validatorRules.confirmorName]" placeholder='请输入确认人'
                       style='width: 100%' />
            </a-form-item>
          </a-col>
          <a-col :span='12' v-if='isAdd===2'>
            <a-form-item label='确认时间' :labelCol='labelCol' :wrapperCol='wrapperCol'>
              <j-date placeholder='确认时间' v-decorator="['confirmorTime',validatorRules.confirmorTime]"
                      :trigger-change='true' style='width: 100%' />
            </a-form-item>
          </a-col>
          <a-col :span='24' v-if='isAdd===2'>
            <a-form-item label='确认意见' :labelCol='labelCol2' :wrapperCol='wrapperCol2'>
              <a-textarea :autoSize='{minRows:4}' v-decorator="['confirmorDes',validatorRules.confirmorDes]"
                          placeholder='请输入专题描述' />
            </a-form-item>
          </a-col>
          <a-col v-if='showFlowSubmitButton' :span='24' style='text-align: center'>
            <a-button @click='submitForm'>提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
    <hf-project-select-modal ref='hfProjectSelectModal' :multiple='false' @hf-pm-project-select='hfPmProjectSelect' />
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import HfProjectSelectModal from '@views/hifar/pm/modules/HfProjectSelectModal'
import moment from 'moment'

export default {
  name: 'HfPmProjectSubjectsForm',
  components: {
    HfProjectSelectModal
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      moment,
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      confirmLoading: false,
      validatorRules: {
        projectShortName: {
          rules: [
            { required: true, message: '请选择项目名称!' }
          ]
        },
        subjectName: {
          rules: [
            { required: true, message: '请输入专题名称!' }
          ]
        },
        playStartTime: {
          rules: [
            { required: true, message: '请选择开始时间!' }
          ]
        },
        playEndTime: {
          rules: [
            { required: true, message: '请选择结束时间!' }
          ]
        },
        confirmorName: {
          rules: [
            { required: true, message: '请选择确认人!' }
          ]
        },
        subjectDes: {
          rules: [
            { required: true, message: '请输入专题描述!' }
          ]
        },
        subjectState: {
          rules: [
            { required: true, message: '请选择状态!' }
          ],
          initialValue: '0'
        },
        estimateWorkday: {
          rules: [
            { required: true, message: '请输入预估人天!' },
            { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!' }
          ],
          initialValue: '0'
        },
        // subjectCords: {
        //   rules: [
        //     { required: false, message: '请输入专题分数!'},
        //     { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!' }
        //   ]
        // },
        realityWorkday: {
          rules: [
            { required: true, message: '请输入实际人天!' },
            { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!' }
          ]
        },
        confirmorTime: {
          rules: [
            { required: true, message: '请选择确认时间!' }
          ]
        },
        confirmorDes: {
          rules: [
            { required: true, message: '请输入确认意见!' }
          ]
        }
      },
      url: {
        add: '/pm/hfPmProjectSubjects/add',
        edit: '/pm/hfPmProjectSubjects/edit',
        queryById: '/pm/hfPmProjectSubjects/queryById'
      },
      fields: [
        'createBy', 'projectId', 'projectCode', 'projectName', 'createTime', 'remarks', 'subjectCode', 'subjectName',
        'playStartTime', 'subjectCords', 'playEndTime', 'subjectDes', 'confirmorName', 'confirmorDes', 'confirmorTime',
        'estimateWorkday', 'realityWorkday', 'subjectState', 'beforeSubjectId'
      ],
      defaultModel: {},
      isAdd: 1 // add 1 ,edit 2
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    add() {
      let defaultObj = {}
      let {
        selectedSubName,
        selectedName,
        selectedKeys,
        selectedSubKeys,
        selectedCode,
        selectedSubCode,
        selectedTreeType
      } = this.defaultModel
      if (selectedTreeType === 'project') {
        defaultObj = { projectName: selectedName, projectId: selectedKeys, projectCode: selectedCode }
      } else if (selectedTreeType === 'subject') {
        defaultObj = {
          projectName: selectedName,
          projectId: selectedKeys,
          projectCode: selectedCode,
          subjectName: selectedSubName,
          subjectCode: selectedSubCode,
          subjectId: selectedSubKeys
        }
      }
      this.edit(defaultObj)
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.model.confirmorName = this.$store.getters.userInfo.username
      this.model.confirmorTime = moment().format('YYYY-MM-DD')
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.fields))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }
      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, this.fields))
    },
    projectShortNameClick() {
      this.$nextTick(() => {
        this.$refs.hfProjectSelectModal.title = '选择项目'
        this.$refs.hfProjectSelectModal.visible = true
      })
    },
    hfPmProjectSelect(data) {
      this.form.setFieldsValue({
        projectId: data.id,
        projectName: data.shortName,
        projectCode: data.code
      })
    }
  }
}
</script>